package cc.wsyw126.java.test;

import cc.wsyw126.java.dto.CSV;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by junpeng.yang on 16/10/15.
 */
public class FileInputOutput {

    public static void main(String[] args) throws IOException {
        long start = System.currentTimeMillis();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String fileName = "test-/Users/yangdp/Desktop/temp/Test数据迁移";
        fileName += dateFormat.format(new Date());
        fileName += "数据迁移";
        fileName += ".csv";

        File file = new File(fileName);
        FileOutputStream fs = new FileOutputStream(file);
        List<CSV> outCSV = new ArrayList<>();
        for (int i = 0; i < 800000; i++) {
            CSV temp = new CSV();
            temp.setResourceType(1);
            temp.setResourceId(2);
            temp.setManagerId(3);
            temp.setId(4);
            temp.setBuId(5);
            outCSV.add(temp);
        }
        System.out.println("outCSV.size() = " + outCSV.size());
        System.out.println("start -System.currentTimeMillis() = " + (start - System.currentTimeMillis()));
        start = System.currentTimeMillis();
        for (CSV csv : outCSV) {
            String s = csv.getId()+","+csv.getBuId()+","+csv.getManagerId()+","+csv.getResourceType()+","+csv.getManagerId();
            fs.write(s.getBytes());
            fs.write("\r\n".getBytes());
        }
        fs.close();
        System.out.println("start -System.currentTimeMillis() = " + (start - System.currentTimeMillis()));
        start = System.currentTimeMillis();



        FileReader io = new FileReader(fileName);
        BufferedReader bf= new BufferedReader(io);


        String s = null;
        List<CSV> reads = new ArrayList<>();
        while((s = bf.readLine())!=null) {
            String[] array = s.split(",");
            CSV csv = new CSV();
            csv.setId(Integer.valueOf(array[0]));
            csv.setBuId(Integer.valueOf(array[1]));
            csv.setManagerId(Integer.valueOf(array[2]));
            csv.setResourceType(Integer.valueOf(array[3]));
            csv.setResourceId(Integer.valueOf(array[4]));
            reads.add(csv);
        }
        bf.close();
        System.out.println("start -System.currentTimeMillis()\" = " + (start - System.currentTimeMillis()));

        System.out.println("reads.size() = " + reads.size());
    }
}
